<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"
    />
    <title>策略模式-传统方式</title>
</head>

<body>
    <strong>策略模式的定义是：定义一系列的算法，把它们一个个封装起来，并且使它们可以相互替换。</strong>

    <script type="text/javascript">
        var performanceS =function(){}
        performanceS.prototype.calculate = function(salary){
            return salary * 4
        }
        var performanceA =function(){}
        performanceA.prototype.calculate = function(salary){
            return salary * 3
        }
        var performanceB =function(){}
        performanceB.prototype.calculate = function(salary){
            return salary * 2
        }

        var Bonus = function (performanceLevel, salary) {
            this.salary = null // 原始工资
            this.strategy = null // 绩效等级对应的策略对象
        }

        Bonus.prototype.setSalary = function(salary){ // 设置金额
            this.salary = salary
        }
        Bonus.prototype.setStrategy = function(strategy){ // 设置策略
            this.strategy = strategy
        }
        Bonus.prototype.getBonus = function(){ // 取得奖金数额
            return this.strategy.calculate(this.salary)
        }

        var bonus = new Bonus()
        bonus.setSalary(4000)
        bonus.setStrategy(new performanceS())
        var money = bonus.getBonus()

        bonus.setSalary(3000)
        var money2 = bonus.getBonus()

        console.log(money)
        console.log(money2)

    </script>

</body>

</html>